﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.SIESystemManager.BLL;

namespace Lamex.SIESystemManager.UI
{
    public partial class FrmImport : DevExpress.XtraEditors.XtraForm
    {
        string tableName = "";
        public FrmImport(string TableName)
        {
            tableName = TableName;
            InitializeComponent();
            DataTable dtcol = BaseFactory<B_IE_SJData_Import>.Instance.GetSysColunmNameList(tableName);
            gcMain.DataSource = dtcol;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在导入数据...", new Size(400, 80)))
                {
                    string path = dlg.FileName;
                    DataTable dt = Lamex.Common.ExecelHelper.ExcelToDataTable(path, 1);
                    DataTable dtcol = BaseFactory<B_IE_SJData_Import>.Instance.GetSysColunmNameList(tableName);
                    DataTable dtSave = new DataTable();
                    dt.TableName = tableName;
                    string sql = "insert into " + tableName + "( ";
                    string sql2 = "select ";
                    string field = "";
                    string field2 = "";
                    foreach (DataRow dr in dtcol.Rows)
                    {
                        dtcol.TableName = dr["table_name"].ToString();
                        if (dr["keyname"].ToString().ToLower() != "key")
                        {
                            int m = 0;
                            foreach (DataColumn d in dt.Columns)
                            {

                                if (dr["column_remark"].ToString() == d.ColumnName)
                                {
                                    m = 1;
                                    field += dr["column_name"].ToString() + ",";
                                    field2 += dr["column_name"].ToString() + ",";
                                    dt.Columns[d.ColumnName].ColumnName = dr["column_name"].ToString();
                                }
                            }
                            //if (m==0)
                            //{
                            //    field += dr["column_name"].ToString() + ",";
                            //    field2 += dr["column_name"].ToString() + ",";
                            //    dt.Columns.Add(dr["column_name"].ToString(), Lamex.Common.DTHelper.dbTypeConvertType(dr["column_type"].ToString()));
                            //} 
                        }

                    }
                    field = field.Trim(',');
                    field2 = field2.Trim(',');
                    sql += field;
                    sql2 += field2;

                    sql += " ) ";
                    sql2 += " from #" + tableName + "";
                    sql = sql + sql2;
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    string errMsg = "";
                    if (BaseBuildSql.ExecuteInsertBulkData("select  " + field + "  into #" + tableName + " from  " + tableName + " where 1!=1", ds, sql,ref errMsg))
                    {
                        XtraMessageBox.Show("成功导入");
                        this.Close();

                    }
                    else
                    {
                        XtraMessageBox.Show(errMsg);
                    }
                }
            }
        }
    }
}